<?php



//header('Refresh: 1; URL=http://www.allpowershop.com.au');
require_once('lib/swift_required.php');
include 'connection.php';
include 'jobsheetspdf.php';

$start_date = $_POST['start_date'];

$end_date = $_POST['end_date'];

/*$query = "SELECT * from pp086_project_times WHERE created_on BETWEEN '".$start_date."' and '".$end_date."'";

$results = mysql_query($query);

$result = mysql_fetch_row($results);

$availability = $result[0];

if ($availability != 0)

{

	//serial number is not availble

	die ("You entered a serial number that has already been registered. Your Serial Number must be unique. Please use the back button and check your entries on the form.");

} 
$query= "INSERT INTO `warranties`(`storename`, `salesperson`, `brand`, `model`, `serialnumber`, `usage`, `purchasedate`, `invoicenumber`, `purchaseprice`, `firstname`, `lastname`, `contractornumber`, `contractortype`, `businessname`, `address`, `suburb`, `postcode`, `state`, `phone`, `email`) VALUES ('".$Storename."','".$Saleperson."','".$Brand."','".$Model."','".$Serialnumber."','".$Usage."','".$Purchasedate."','".$Invoicenumber."','".$Price."','".$FirstName."','".$Surname."','".$Contractornumber."','".$Contractortype."','".$Businessname."','".$Address."','".$Suburb."','".$Postcode."','".$State."','".$Phone."','".$Email."')";

mysql_query($query) or die ("The system has encountered an error. Please use the back button to return to your form and try submit again.");

	$EmailSubject = 'Extended Warranty';

	$mailheader = "From: info@allpower.com.au \r\n";  

	$mailheader .= "Content-type: text/html; charset=iso-8859-1 \r\n";

	$mailheader .= "To: allpoweraustralia@gmail.com \r\n";

	$MESSAGE_BODY = "A new Warranty Registration has been made with the following details:<br /><br />

	First Name: $FirstName<br />

    Last Name: $Surname<br />

    Contractor Number: $Contractornumber<br />

	Contractor Type: $Contractortype<br />

	Business Name: $Businessname<br />

	Street Address: $Address<br />

	Suburb: $Suburb<br />

	Postcode: $Postcode<br />

	State: $State<br />

	Phone: $Phone<br />

	Email: $Email<br />

	Store Name: $Storename<br />

	Sales Person: $Saleperson<br />

	Brand: $Brand<br />

	Model: $Model<br />

	Serial Number: $Serialnumber<br />

	Usage: $Usage<br />

	Purchase Date: $Purchasedate<br />

	Invoice Number: $Invoicenumber<br />

	Price: $Price<br />";

    mail("", $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("There was an error sending the mail");*/

	createPdf($start_date, $end_date);
	
	/*$filename = 'warranty_'.$Serialnumber.'.pdf';
	?>
	<script type="text/javascript">
	var filename = "<?php echo $filename; ?>";
	window.open (filename)</script>
	
	<?php
	
	if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { 
  $eol="\r\n"; 
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { 
  $eol="\r"; 
} else { 
  $eol="\n"; 
} ?> 

<?php 
# File for Attachment 
$f_name=$filename;    // use relative path OR ELSE big headaches. $letter is my file for attaching. 
$handle=fopen($f_name, 'rb'); 
$f_contents=fread($handle, filesize($f_name)); 
$f_contents=chunk_split(base64_encode($f_contents));    //Encode The Data For Transition using base64_encode(); 
$f_type=filetype($f_name); 
fclose($handle); 
# To Email Address 
$emailaddress=$Email; 
# Message Subject 
$emailsubject="Allpower Warranty Certificate"; 
# Message Body 
$body="Please find attached a copy of your warranty certificate"; 

# Common Headers 
$headers .= 'From: info@eyet.com.au'.$eol; 
//$headers .= 'Reply-To: Allpower <info@allpower.com.au>'.$eol; 
//$headers .= 'Return-Path: Allpower <info@allpower.com.au>'.$eol;// these two to set reply address 
//$headers .= "Message-ID:<".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol; 
//$headers .= "X-Mailer: PHP v".phpversion().$eol;           // These two to help avoid spam-filters 
# Boundry for marking the split & Multitype Headers 
$mime_boundary=md5(time()); 
$headers .= 'MIME-Version: 1.0'.$eol; 
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol.$eol; 
//$msg = "";
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol; 

# HTML Version 
$headers .= "--".$mime_boundary.$eol; 
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$headers .= "You are receiving this message because you have recently registered a product warranty on the Allpower website.<br /><br />Find attached a copy of your warranty certificate.<br /><br />If you have any queries, you can get in touch with Allpower by emailing info@allpower.com.au.".$eol.$eol; 

# Attachment 
$headers .= "--".$mime_boundary.$eol; 
$headers .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;   // sometimes i have to send MS Word, use 'msword' instead of 'pdf' 
$headers .= "Content-Transfer-Encoding: base64".$eol; 
$headers .= "Content-Disposition: attachment;".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! 
$headers .= $f_contents.$eol.$eol;  

# Finished 
$headers .= "--".$mime_boundary."--".$eol.$eol;   // finish with two eol's for better security. see Injection. 

# SEND THE EMAIL  
mail($emailaddress, $emailsubject, "", $headers)  or die ("There was an error sending the second mail"); 


		
	/*echo '<script type="text/javascript">

	alert("Thank you for registering,  your details have been forwarded to our Customer Services Team who will forward your Warranty confirmation.")
    
    </script>';*/
	
	/*$pdffile = $_SERVER['DOCUMENT_ROOT'].'/tcpdf/warranty_certificate_'.$FirstName.'_'.$Surname.'.pdf';
	$open = fopen($pdffile, 'r');
	$warranty_certificate = 'warranty_certificate_'.$FirstName.'_'.$Surname.'.pdf';
	

if (!isset ($_POST['addanother']))

{

printf ("<script type=\"text/javascript\">
function redirectUser(){
  window.location = \"http://www.allpower.com.au\";
}
</script>
</head>
<body>
<h3>You have registered your Warranty Certificate.</h3>
To download your warranty certificate, please allow pop-ups on your browser. Alternatively <a href=".$filename." target=\"_blank\">click here</a>. <br /><br /> A copy of the warranty certificate has been sent to your email address. If you can't see it, please check your spam folder.<br /><br />To continue to the allpower website <a href=\"http://www.allpower.com.au\">click here.</a>");

}

else 

{

printf("<script>location.href='http://www.allpowershop.com.au/shop/pages.php?pageid=13&storename=$Storename&saleperson=$Saleperson&brand=$Brand&model=$Model&serialnumber=$Serialnumber&usage=$Usage&purchasedate=$Purchasedate&invoicenumber=$Invoicenumber&price=$Price&firstname=$FirstName&surname=$Surname&contractornumber=$Contractornumber&contractortype=$Contractortype&businessname=$Businessname&address=$Address&suburb=$Suburb&postcode=$Postcode&state=$State&phone=$Phone&email=$Email&selected=$selected'</script>");

}*/

?>